#if !defined(SingletonHeader)
#define SingletonHeader

template <class T> class SingletonMaintainer;

template <class T>
class Singleton {
public:
  T &operator () () {

    // Avoids g++ optimization which would skip instantiating the maintainer
    maintainer.dummy ();

    return *pointer;
  }

  static SingletonMaintainer<T> maintainer;
  static T *pointer;
};

template <class T>
class SingletonMaintainer {
public:
  SingletonMaintainer () {
    Singleton<T>::pointer = new T;
  }
  ~SingletonMaintainer () {
    delete Singleton<T>::pointer;
  }

  void dummy () {
  }
};

template <class T> SingletonMaintainer<T> Singleton<T>::maintainer;

template <class T> T* Singleton<T>::pointer = 0;

#endif
